﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace T8task2
{
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        
        public Form1()
        {
            InitializeComponent();
        }

        private void ConnDatabase()
        {
            string constr = "server=.;database=Games;Integrated Security=true";
            SqlConnection conn = new SqlConnection(constr);
            //查询两张表
            string sql = "select * from [Categories];select * from [Products]";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);

            da.Fill(ds);

            conn.Close();
            conn.Dispose();

        }
        private void ShowCategoryList()
        {
            //获得类别数据
            DataTable dt = ds.Tables[0];
            //创建根
            TreeNode root = new TreeNode("游戏类别");
            root.Tag = 0;//保存编号
            //将根连接到TreeView
            treeView1.Nodes.Add(root);
            foreach (DataRow row in dt.Rows)
            {
                
                string name = Convert.ToString(row["Name"]);
                //新节点
                TreeNode node = new TreeNode(name);

                //类型编号
                node.Tag = Convert.ToInt32(row["ID"]);
                root.Nodes.Add(node);
            }
            treeView1.ExpandAll();
        }
        private void ShowGameList()
        {
            DataTable dt = ds.Tables[1];
            
            //listView1.
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            ConnDatabase();
            ShowCategoryList();
            ShowGameList();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            /*TreeNode selectNode = treeView1.SelectedNode;
            if (selectNode.Text != "游戏类别")
            {
                MessageBox.Show(selectNode.Text);
            }*/
            TreeNode node = e.Node;
            int id = Convert.ToInt32(node.Tag);
            if (id != 0)
            {
                DataView dv = ds.Tables[1].DefaultView;
                //
                dv.RowFilter = string.Format("CategoryId={0}", id);
                listView1.DataSource = dv;
            }
        }
    }
}
